package com.banzhi.shop.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.banzhi.shop.MyApp;
import com.banzhi.shop.R;
import com.banzhi.shop.entity.HomeFrg.goods_data;
import com.banzhi.shop.utils.NumberUtils;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by LeiHe on 2017/4/26.
 */

public class RecyclerViewAda extends RecyclerView.Adapter<RecyclerViewAda.MyViewHolder> {

    private List<goods_data> mlist;
    private Context mcontext;
    private LayoutInflater inflater;

    public RecyclerViewAda(List<goods_data> mlist, Context mcontext) {
        this.mlist = mlist;
        this.mcontext = mcontext;
        this.inflater = LayoutInflater.from(mcontext);
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.recycleview_items, parent, false);
        MyViewHolder holder = new MyViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        final goods_data data = mlist.get(position);

        holder.tv.setText(mlist.get(position).getTitle());
        holder.tvprice.setText(NumberUtils.DecimalFormat(mlist.get(position).getMarketprice()));
        holder.tvliyun.setText(NumberUtils.DecimalFormat(mlist.get(position).getProfit()));
        if ("1".equals(mlist.get(position).getShowtotal())) {
            holder.llkucun.setVisibility(View.VISIBLE);
            holder.tvkucun.setText(mlist.get(position).getTotal());
        } else {
            holder.llkucun.setVisibility(View.GONE);
        }
        holder.tvpeoplenum.setText(mlist.get(position).getSharebtn());
        if ("0".equals(data.getIcon())) {
            holder.ivadddianpu.setImageResource(R.mipmap.jian1);
        } else if ("1".equals(data.getIcon())) {
            holder.ivadddianpu.setImageResource(R.mipmap.jia1);
        }

        Picasso.with(MyApp.context).load(mlist.get(position).getThumb()).placeholder(R.mipmap.zhanweitu2).into(holder.imageView);
        //判断是否设置了监听器
        if (mOnItemClickListener != null) {
            //为ItemView设置监听器
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = holder.getLayoutPosition(); // 1
                    mOnItemClickListener.onItemClick(holder.itemView, position); // 2
                }
            });
            holder.ivadddianpu.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if ("0".equals(data.getIcon())) {
                        mOnItemClickListener.ondeleteClick(data, position, data.getIcon());
                    } else if ("1".equals(data.getIcon())) {
                        mOnItemClickListener.OnAddDianpuClick(data, position, data.getIcon());
                    }
                }
            });
            holder.ivaddimage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnItemClickListener.OnAddImageClick(data);
                }
            });
            holder.ivfenxiang.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnItemClickListener.OnShareClick(data, position);
                }
            });
        }

    }

    @Override
    public int getItemCount() {
        return mlist.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView tv;
        TextView tvprice;
        TextView tvliyun;
        TextView tvkucun;
        TextView tvpeoplenum;
        ImageView imageView;
        ImageView ivadddianpu;
        ImageView ivaddimage;
        ImageView ivfenxiang;
        LinearLayout llkucun;

        public MyViewHolder(View itemView) {
            super(itemView);
            tv = (TextView) itemView.findViewById(R.id.tv_home_content);
            tvprice = (TextView) itemView.findViewById(R.id.tv_price);
            tvliyun = (TextView) itemView.findViewById(R.id.tv_liyun);
            tvkucun = (TextView) itemView.findViewById(R.id.tv_kucun);
            tvpeoplenum = (TextView) itemView.findViewById(R.id.tv_Peoplenum);
            ivadddianpu = (ImageView) itemView.findViewById(R.id.iv_add_dianpu);
            ivaddimage = (ImageView) itemView.findViewById(R.id.iv_add_image);
            ivfenxiang = (ImageView) itemView.findViewById(R.id.iv_fenxiang);
            imageView = (ImageView) itemView.findViewById(R.id.img_home);
            llkucun = (LinearLayout) itemView.findViewById(R.id.ll_kucun);

        }
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);

        void OnAddDianpuClick(Object o, int position, String icon);

        void OnAddImageClick(Object o);

        void OnShareClick(Object o, int positon);

        void ondeleteClick(Object o, int position, String icon);
    }

    public interface OnItemLongClickListener {
        void onItemLongClick(View view, int position);
    }

    private OnItemClickListener mOnItemClickListener;
    private OnItemLongClickListener mOnItemLongClickListener;

    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    public void setOnItemLongClickListener(OnItemLongClickListener mOnItemLongClickListener) {
        this.mOnItemLongClickListener = mOnItemLongClickListener;
    }

}
